package com.example.demo.modules.base.serviceimpl;

import com.example.demo.modules.base.Mapper.ResourceMapper;
import com.example.demo.modules.base.dao.UserDao;

import com.example.demo.modules.base.Mapper.UserRoleMapper;
import com.example.demo.modules.base.entity.Resource;
import com.example.demo.modules.base.entity.Role;
import com.example.demo.modules.base.entity.User;
import com.example.demo.modules.base.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户接口实现
 * @author yw
 */
@Slf4j
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public UserDao getRepository() {
        return userDao;
    }

    @Override
    public User findByUsername(String username) {

        List<User> list=userDao.findByUsername(username);
        if(list!=null&&list.size()>0){
            User user = list.get(0);
            // 关联部门
            // 关联角色
            List<Role> roleList = userRoleMapper.findByUserId(user.getId());
            user.setRoles(roleList);
            // 关联权限菜单
            List<Resource> resourceList = resourceMapper.findByUserId(user.getId());
            user.setResources(resourceList);
            return user;
        }
        return null;
    }
}